﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.Utils.Menu;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using ERP_RORZE_ORDER.BizLogic;
using ERP_RORZE_ORDER.Common;
using ERP_RORZE_ORDER.Common.ValueObject;
using ERP_RORZE_ORDER.OrderObject.Forms;
using ERP_RORZE_TOOL.BizLogic;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_TOOL.DBAccess;

namespace ERP_RORZE_ORDER.OrderObject.UserControls
{
    public partial class ucOrderData : DevExpress.XtraEditors.XtraUserControl
    {
        DXPopupMenu menu;
        private string query = "Sp_243_Rpt_OrderData";
        private int year = DateTime.Now.Year;
        #region Handle methods
        public ucOrderData()
        {
            InitializeComponent();
            InitPopupMenu();
            InitDateRangeMenu();
            BindDatatoGrid(year);
            //gridView1.BestFitColumns();
            this.dropbtExport.DropDownControl = menu;

        }

        private void btRefresh_Click(object sender, EventArgs e)
        {
            BindDatatoGrid(year);
        }

        private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
        {
            if (gridView1.RowCount != 0)
                return;

            StringFormat drawFormat = new StringFormat();

            drawFormat.Alignment = drawFormat.LineAlignment = StringAlignment.Center;

            e.Graphics.DrawString("No items exist in this view", e.Appearance.Font, SystemBrushes.ControlDark, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height), drawFormat);

        }


        #endregion

        #region Private methods
        public void BindDatatoGrid(int y)
        {
            this.year = y;
            gridControl1.DataSource = BaseDAO.ExecuteQuery(query + "  " + y);
            gridView1.ViewCaption = "Dữ liệu đặt hàng " + y.ToString();

            gridView1.Columns["Quantity"].DisplayFormat.FormatType = FormatType.Numeric;
            gridView1.Columns["Quantity"].DisplayFormat.FormatString = "n0";

            gridView1.Columns["Unit Price"].DisplayFormat.FormatType = FormatType.Numeric;
            gridView1.Columns["Unit Price"].DisplayFormat.FormatString = "n2";

            gridView1.Columns["Amount"].DisplayFormat.FormatType = FormatType.Numeric;
            gridView1.Columns["Amount"].DisplayFormat.FormatString = "n2";

            gridView1.Columns["Ex. rate"].DisplayFormat.FormatType = FormatType.Numeric;
            gridView1.Columns["Ex. rate"].DisplayFormat.FormatString = "n2";

            gridView1.Columns["AmountUSD"].DisplayFormat.FormatType = FormatType.Numeric;
            gridView1.Columns["AmountUSD"].DisplayFormat.FormatString = "n2";

            gridView1.Columns["Deadline"].DisplayFormat.FormatType = FormatType.DateTime;
            gridView1.Columns["Deadline"].DisplayFormat.FormatString = "dd/MM/yyyy";
            

            gridView1.BestFitColumns();
        }

        protected virtual void InitDateRangeMenu()
        {
            int maxYear = (int)BaseDAO.ExecuteScalar("select max(year([Date])) from [243_Order]");
            int minYear = (int)BaseDAO.ExecuteScalar("select min(year([Date])) from [243_Order]");

            this.year = maxYear;
            for (int year = maxYear; year >= minYear; year--)
            {
                Utilities.ComboboxItem item = new Utilities.ComboboxItem();
                item.Text = year.ToString();
                item.Value = year.ToString();
                cmbYear.Items.Add(item);
            }

            cmbYear.SelectedItem = cmbYear.Items[0];
        }


        protected virtual void InitPopupMenu()
        {
            menu = new DXPopupMenu();
            menu.Items.Add(new DXMenuItem("Export all", new EventHandler(OnItemClickExportAll), null, null, null, null));
            menu.Items.Add(new DXMenuItem("Export selected row(s)", new EventHandler(OnItemClickExportRows), null, null, null, null));

        }
        void OnItemClickExportAll(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 1, true);
        }
        void OnItemClickExportRows(object sender, EventArgs e)
        {
            Utilities.ExportToExcel(gridView1, 0, true);
        }

        #endregion


        private void cmbYear_SelectionChangeCommitted(object sender, EventArgs e)
        {
            BindDatatoGrid(int.Parse(cmbYear.Text));

        }


    }
}
